package com.hexinfo.dmpro.data.transfer.common.controller;

import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.data.transfer.common.service.ICdhMetadataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/api/cdh/metadata")
@Api(value = "cdhMetadata", tags = "CDH元数据库查询")
public class CdhMetadataController {

    private final ICdhMetadataService cdhMetadataService;

    @ApiOperation(value = "库集合", notes = "库集合")
    @GetMapping(value = "/databases")
    @AnonymousAccess
    public ResponseEntity<Object> databases() {
        return ResponseEntity.ok(cdhMetadataService.queryAllDatabase());
    }

    @ApiOperation(value = "表集合", notes = "表集合")
    @GetMapping(value = "/tables/{database}")
    @AnonymousAccess
    public ResponseEntity<Object> tables(@PathVariable("database") String database) {
        return ResponseEntity.ok(cdhMetadataService.queryTableListByDatabase(database));
    }

    @ApiOperation(value = "分区集合", notes = "分区集合")
    @GetMapping(value = "/partitions/{database}/{table}")
    @AnonymousAccess
    public ResponseEntity<Object> partitions(@PathVariable("database") String database, @PathVariable("table") String table) {
        return ResponseEntity.ok(cdhMetadataService.queryPartitionList(database, table));
    }

    @ApiOperation(value = "字段集合", notes = "字段集合")
    @GetMapping(value = "/columns/{database}/{table}")
    @AnonymousAccess
    public ResponseEntity<Object> columns(@PathVariable("database") String database, @PathVariable("table") String table) {
        return ResponseEntity.ok(cdhMetadataService.queryColumnList(database, table));
    }
}
